package com.googlecode.jsimplemarket.wicket.components.grid;

import com.googlecode.jsimplemarket.repository.GenericOrder;
import com.googlecode.jsimplemarket.repository.GenericRestriction;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 *
 * @author Alexandru.Codescu
 */
public class Grid<T extends Serializable> extends Panel {

    private List<GenericRestriction> genericProperties;
    private List<GenericOrder> genericOrders;
    private ActionButtons actionButtons;
    private DataTable dataTable;
    private FilterToolbar filterToolbar;

    public Grid(String id,
            List<ColumnModel> gridColumns,
            final Class<T> persistentClass,
            final GridAction gridAction,
            final ActionButtons actionButtons) {
        super(id);
        init(gridColumns, persistentClass, gridAction, null, actionButtons);
    }

    public Grid(String id,
            List<ColumnModel> gridColumns,
            final Class<T> persistentClass,
            final GridAction gridAction,
            final Component component,
            final ActionButtons actionButtons) {
        super(id);
        init(gridColumns, persistentClass, gridAction, component, actionButtons);
    }

    public Grid(String id,
            List<ColumnModel> gridColumns,
            final Class<T> persistentClass,
            final GridAction gridAction,
            final ActionButtons actionButtons,
            final List<GenericRestriction> genericProperties,
            final List<GenericOrder> genericOrders) {
        super(id);
        this.genericProperties = genericProperties;
        this.genericOrders = genericOrders;
        init(gridColumns, persistentClass, gridAction, null, actionButtons);
    }

    private List<IColumn<T, String>> createColumns(final List<ColumnModel> gridColumns) {
        List<IColumn<T, String>> columns = new ArrayList<IColumn<T, String>>();
        for (final ColumnModel gridColumn : gridColumns) {
            columns.add(new PropertyColumn<T, String>(new Model<String>(gridColumn.getLabelName()), gridColumn.getProperty(), gridColumn.getProperty()) {
                @Override
                public String getCssClass() {
                    if (gridColumn.getCssClass() != null) {
                        return gridColumn.getCssClass();
                    }
                    return super.getCssClass();
                }
            });
        }
        return columns;
    }

    private void addActionButtons(List<IColumn<T, String>> columns, final ActionButtons actionButtons) {
        if ((actionButtons != null) && (actionButtons.getModifyButtons() != null) && (!actionButtons.getModifyButtons().isEmpty())) {
            columns.add(new AbstractColumn<T, String>(new Model<String>("Actiuni")) {
                @Override
                public void populateItem(Item<ICellPopulator<T>> cellItem, String componentId,
                        IModel<T> model) {

                    cellItem.add(new ActionPanel(componentId, model, actionButtons.getModifyButtons()));
                }
            });
        }
    }
    
    public void setColumns(final List<ColumnModel> gridColumns) {
        List<IColumn<T, String>> columns = createColumns(gridColumns);
        addActionButtons(columns, actionButtons);
        dataTable.getColumns().clear();
        dataTable.getColumns().addAll(columns);
        filterToolbar.setColumns(gridColumns);
    }

    private void init(List<ColumnModel> gridColumns,
            final Class<T> persistentClass,
            final GridAction gridAction,
            final Component component,
            final ActionButtons actionButtons) {
        this.actionButtons = actionButtons;
        if (genericProperties != null) {
            List<GenericRestriction> lista = new ArrayList<GenericRestriction>();
            lista.addAll(genericProperties);
            genericProperties = lista;
        }
        this.setOutputMarkupId(true);

        List<IColumn<T, String>> columns = createColumns(gridColumns);
        addActionButtons(columns, actionButtons);

        GridDataProvider<T> dataProvider = new GridDataProvider<T>(persistentClass, gridAction, genericProperties, genericOrders);
        dataTable = new DefaultDataTable<T, String>("table", columns, dataProvider, GridAction.DEFAULT_PAGE_SIZE);
        filterToolbar = new FilterToolbar<T>(dataTable, gridColumns, dataProvider, this);
        dataTable.addTopToolbar(filterToolbar);
        dataTable.setOutputMarkupId(true);
        if (actionButtons != null) {
            dataTable.addBottomToolbar(new ActionToolbar(dataTable, component, actionButtons.getInsertButton()));
        }

        add(dataTable);
    }
}
